<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<%@ include file="/WEB-INF/views/include/easyui.jsp"%>
</head>
<body>
	<div id='Loading' style="position:absolute;z-index:1000;top:0px;left:0px;width:100%;height:100%;background:#FFFFFF;text-align:center;padding-top: 20%;"><font color="#15428B" size="3px"></font></div>

	<div>
		<table id="personTreeGrid"></table>  
	</div>
	
	<div id="addOrUpdate" class="easyui-dialog" closed="true">
		<form id="addOrUpdateForm" method="post">
			<table width="100%" height="70%">
				<tr>
					<td>类别:<select class="easyui-combobox" style="width:148px;" id="Type" name="Type">
						<option value="0">分组</option>
						<option value="1">用户</option>
					</select></td>
					<td>名称:<input type="text" id="Name" name="Name" style="width:145px;"></input></td>
				</tr>
				<tr>
					<td>邮箱:<input type="text" id="Email" name="Email" style="width:145px;"></input></td>
					<td>电话:<input type="text" id="Phone" name="Phone" style="width:145px;"></input></td>
				</tr>
			</table>
		</form>
		<div id="quy-buttons" style="text-align: center;border-top:1px solid #ccc;padding:10px;">
			<a onclick="addOrUpdate()" class="easyui-linkbutton" iconCls="icon-ok" >提交</a>
			<a style="margin-left: 10px;" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#addOrUpdate').dialog('close');">取消</a>
		</div>
	</div> 
	<script>
	var index;
	 $('#personTreeGrid').treegrid({    
		    url:'<%=path %>/personalMessage/initpersonal',    
		    idField:'id',    
		    treeField:'groupName',
		    columns:[[    
		        {field:'groupName',title:'组名',width:180},    
		        {field:'email',title:'邮箱',width:180},    
		        {field:'phone',title:'电话',width:180}    
		    ]],
		    toolbar: [{
				iconCls: 'icon-add',
				text:'新增',
				handler: function(){
					index=1;
					var node = $('#personTreeGrid').treegrid('getSelected');
					if(node!=null){
						var nodetype=node.person;
						if(nodetype=="1"){
							$.messager.show({
								msg : '用户下不能再添加用户！',
								title : '提示'
							});
							return;
						 }
						$('#addOrUpdate').dialog('open');
					}
				}
			},'-',{
				iconCls: 'icon-edit',
				text:'编辑',
				handler: function(){
					index=0;
					var node = $('#personTreeGrid').treegrid('getSelected');
					if(node!=null){
						$('#addOrUpdate').dialog('open');
						$('#Email').val(node.email);
						$('#Phone').val(node.phone);
						$('#Name').val(node.groupName);
						//$('#Type').combobox('setValue',node.person);
						if(node.person=='1'){
							$("#Type").val("1");
							$("#Type").find("option[text='用户']").attr("selected",true);
						}else{
							$("#Type").val("0");
							$("#Type").find("option[text='分组']").attr("selected",true);
						}
					}
				}
			},'-',{
				iconCls: 'icon-remove',
				text:'删除',
				handler: function(){
					del();
				}
			}]
		}); 
	 
	 //弹出框出舒华
	 $('#addOrUpdate').dialog({    
		    title: 'addOrUpdate',    
		    width:  400,    
		    height: 150,    
		    modal: true
		});
	 
	 
	 //新增编辑
	 function addOrUpdate(){
		 //获取选中行
		 var node = $('#personTreeGrid').treegrid('getSelected');
		 var nodeid=node.id;
		 //var nodes=$('#personTreeGrid').treegrid('getParent', nodeid);
		 $('#addOrUpdateForm').form('submit', {   
			 url:'<%=path%>/personalMessage/addOrUpdate',  
			 onSubmit: function(param){
				 param.ID=nodeid; 
				 param.index=index;
			 }, 
			 success: function(data){    
				 var r = eval('(' + data + ')');
				 if(r.success){
					 $.messager.show({
	 						msg : r.msg,
	 						title : '提示'
	 					});
					 $("#personTreeGrid").treegrid('reload');
					 $('#addOrUpdate').dialog('close');
					 $('#addOrUpdateForm')[0].reset();
				 }else{
					 $.messager.show({
	 						msg : r.msg,
	 						title : '提示'
	 				});
				 }
			 }    
		});  
	 }
	 
	 function del(){
		 var node = $('#personTreeGrid').treegrid('getSelected');
		 if(node!=null){
			 $.ajax({
				 url:'<%=path%>/personalMessage/remove',
				 type:'post',
				 data:{'ID':node.id,'type':node.person},
				 async:false,
				 success:function(data){
					 var r = eval('(' + data + ')');
					 if(r.success){
						 $.messager.show({
		 						msg : r.msg,
		 						title : '提示'
		 					});
						 $("#personTreeGrid").treegrid('reload');
					 }else{
						 $.messager.show({
		 						msg : r.msg,
		 						title : '提示'
		 					});
					 }
				 }
			 });
			 
		 }
	 }
	</script>
</body>
	
</html>